﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace RenameTracker
{
    /// <summary>
    /// Subclass that's optimized for when you're doing a wholesale wipe of the collection
    /// Note: if you're just doing surgical updates this will be a lot slower than a regular ObservableCollection
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ResetCollection<T> : ObservableCollection<T>
    {
        private bool swallowEvents = false;

        /// <summary>
        /// Clear the collection and populate it with a new set of items
        /// </summary>
        /// <param name="newList"></param>
        public void ResetTo(IEnumerable<T> newList)
        {
            swallowEvents = true;

            base.ClearItems();
            foreach (T item in newList)
            {
                base.Add(item);
            }

            swallowEvents = false;
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (!swallowEvents)
            {
                base.OnCollectionChanged(e);
            }
        }
    }
}
